const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bgPic: null,
    picChoosed: false
  },

  assignPicChoosed() {
    if (this.data.bgPic) {
      this.setData({
        picChoosed: true
      })
    } else {
      this.setData({
        picChoosed: false
      })
    }
  },
  getAvatar() {
    var th=this;
    console.log("登录")
    wx.getUserProfile({
      desc: '用于完善会员资料', 
      // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          oppenid: res.cloudID
        })
        wx.cloud.callFunction({
          name: 'imgSecCheck',
          data: {
            img: res.userInfo.avatarUrl.substring(0, res.userInfo.avatarUrl.length - 3) + '0'
          }
        }).then(
          imgRes => {
            if (imgRes.result.errCode == 0) {
              this.setData({
                bgPic: res.userInfo.avatarUrl.substring(0, res.userInfo.avatarUrl.length - 3) + '0',
              });
              this.assignPicChoosed();
            } else {
              wx.showToast({
                title: '您的头像不符合要求，请换头像!',
                icon: 'none',
                duration: 2000
              })
            }
          }
        )
        let that = th;
    wx.cloud.callFunction({
      name: 'getUser',
    }).then(res => {
      console.log(res)
      if (res.result.data.length == 0) {
        console.log("hubu")
        wx.cloud.callFunction({
          name: 'addUser',
          data: {
            nickName: that.data.userInfo.nickName,
            gender: that.data.userInfo.gender,
            language: that.data.userInfo.language,
            city: that.data.userInfo.city,
            province: that.data.userInfo.province,
            country: that.data.userInfo.country,
            avatarUrl: that.data.userInfo.avatarUrl
          },
    
        }).then(addres => {
          console.log(addres)
          that.setData({
            userInfo: that.data.userInfo
          })
          wx.setStorageSync('userInfo', JSON.stringify(that.data.userInfo))
        })
      } else {
        console.log(res.result)
        that.setData({
          userInfo: res.result.data[0]
        })
        wx.setStorageSync('userInfo', JSON.stringify(res.result.data[0]))
      }
    })
      },
      fail: (res)=>{
        console.log(res)
      }
    })

  },
  chooseImage(from) {
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: [from.target.dataset.way],
      success: (res) => {
        // 校验一张图片是否含有违法违规内容
        wx.cloud.callFunction({
          name: 'imgSecCheck',
          data: {
            img: res.tempFilePaths[0]
          }
        }).then(
          imgRes => {
            if (imgRes.result.errCode == 0) {
              var tempFilePaths = res.tempFilePaths;
              this.setData({
                bgPic: res.tempFilePaths[0]
              });
              this.assignPicChoosed();
            } else {
              wx.showToast({
                title: '图片不符合要求，请换头像!',
                icon: 'none',
                duration: 2000
              })
            }
          }
        )
      },
      fail: (res) => {
        this.assignPicChoosed();
      },
      complete: (res) => {
        this.assignPicChoosed();
      },
    })
  },
  nextPage() {
    app.globalData.bgPic = this.data.bgPic;
    wx.navigateTo({
      url: '../imageeditor/imageeditor',
    })
  }
})